require(['./config'], () => {
    require(['swiper', 'template', 'request','header', 'footer', 'jquery','cbl'], (Swiper,template,req) => {
        class Index {
            constructor() {
                this.getBannerData();
                this.getIndexData();
            }
            init() {
                var swiper = new Swiper('.swiper-container', {
                    pagination: '.swiper-pagination',
                    nextButton: '.swiper-button-next',
                    prevButton: '.swiper-button-prev',
                    slidesPerView: 1,
                    paginationClickable: true,
                    spaceBetween: 30,
                    loop: true,
                    autoplay: 2000,
                })
                $(".swiper-container").mouseenter(()=>{
                    swiper.stopAutoplay();
                })
                $(".swiper-container").mouseleave(()=>{
                    swiper.startAutoplay();
                })
            }
            getBannerData(){
                $.ajax({
                    url: '/lib/indexBannerMock.json',
                    dataType:'json'
                })
                .then(res => {
                    if(res.code === 200){
                        const list = res.result_body;
                        const html1 = template('bannerTemplate',{list});
                        $('.banner_swiperWrapper').html(html1);
                        this.init();
                    }
                })
            }

            //获取数据
            getIndexData(){
                req.getIndexData()
                    .then(res => {
                        this.render(res.data.mods);
                    })
            }
            
            //渲染数据
            render(list){
               const cateId6 = list[6].link.split('/')[4].split('?')[1];
               const cateId16 = list[16].link.split('/')[4].split('?')[1];
               $(".content").html(template("contentData",{list,cateId6,cateId16}))
               $(".con1 .c1-pic a").click(() => {
                const productId = list[4].pic.items.link.split('/')[5];
                location.href = `/html/details.html?${productId}`;
               })
               $(".con2 .pic").click(() => {
                const productId = list[10].list[0].link.split('/')[5];
                location.href = `/html/details.html?${productId}`;
               })
               $(".con3 .swp").click(() => {
                const productId = list[14].list[0].link.split('/')[5];
                location.href = `/html/details.html?${productId}`;
               })
               $(".con4 .pic").click(() => {
                const productId = list[20].list[0].link.split('/')[5];
                location.href = `/html/details.html?${productId}`;
               })
            }
        }
        return new Index();
    })
})

